/***********************
*DESC:格式化配置数据，封装为满足协议的JSON数据
*Author:wff 
*Date:2015-05-13
*
************************/
var CurrDevObj = [];
var CurrAppObj = [];
var CurrDevPathObj = [];
var CurrDevReactorObj = [];
var CurrAppHomeObj = [];
var CurrAppScenesObj = [];
var CurrAppAlarmsObj = [];
var $devXmlData,$appXmlData;
var isDevReactor = false;
var isAppSence = false;
function SaveCfgAction(currusername)
{
		CurrDevObj = [];
		CurrAppObj = [];
		CurrDevPathObj = [];
		CurrDevReactorObj = [];
		CurrAppHomeObj = [];
		CurrAppScenesObj = [];
		CurrAppAlarmsObj = [];
		if(AppendAllData() == true)
		{
			if(UpdatadevXml() == false)
				return;
			//UpdataappXml();
				
		}
}
function UpdatadevXml(){
	
	var UpdatadevXmlStatus = false;
	var xotree = new XML.ObjTree();	
	var dataxml = formatXml(xotree.writeXML(eval(CurrDevObj[0])));
	
	$devXmlData = dataxml;
	$.ajax({                 
				 
				url : '../orgMgr/devCfg/saveDevLogicCfg',				
                type: 'post',  
                dataType: 'json',
				data: {data: $devXmlData,dnid: currusername},
                timeout: 100000,  
                cache: false,				                
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert("获取参数超时");
					
					return UpdatadevXmlStatus;
				},    
                success:  function(data) {
					//UpdataappXml();
					if(data.resultCode != '10000'){
						
						bootbox.alert(data.resultMsg, function() {					
						
						});	
						return 	UpdatadevXmlStatus;
					}
					else{
						UpdataappXml();
						/*UpdatadevXmlStatus = true;
						bootbox.alert(data.resultMsg, function() {
						
						});	
						return 	UpdatadevXmlStatus;*/					
					}
					 
				}
				   
	})
}
function UpdataappXml(){
	
	var UpdataappXmlStatus = false;
	var xotree = new XML.ObjTree();	
	var dataxml = formatXml(xotree.writeXML(eval(CurrAppObj[0])));
	
	$appXmlData = dataxml;
	$.ajax({                 
				 
				url : '../orgMgr/devCfg/saveDevViewCfg?',				
                type: 'post',  
                dataType: 'json',
				data: {data: $appXmlData,dnid: currusername},
                timeout: 100000,  
                cache: false,				                
				error : function() {
					alert("获取参数超时");
					return UpdataappXmlStatus;
				},    
                success:  function(data) {
				
					if(data.resultCode != '10000'){
						
						bootbox.alert(data.resultMsg, function() {					
						return;
						});
						return UpdataappXmlStatus;
						
					}
					else{
						UpdataappXmlStatus = true;
						bootbox.alert(data.resultMsg, function() {
						return;
						});	
						return UpdataappXmlStatus;
												
					}
					 
				}
				   
	})
}
function AppendAllData()
{
	/*if(EncapsulateALLData() == 0)
	{
		alert("您还未配置任何参数，请配置后再保存");
		return false;
	}*/
	if(EncapsulateALLData() == 1){
	
	if(CurrDevReactorObj.length != 0){
		CurrDevObj.push({"conf": {"path": {"i": CurrDevPathObj},"reactor": {"trigger": CurrDevReactorObj}}});
	}
	else{
		CurrDevObj.push({"conf": {"path": {"i": CurrDevPathObj}}});
	}
	
	if(CurrAppScenesObj.length != 0 && CurrAppAlarmsObj.length != 0){
		CurrAppObj.push({"conf": {"home": {"floor": CurrAppHomeObj},"scenes": {"sceset": CurrAppScenesObj},"alarms": {"alarm": CurrAppAlarmsObj}}});
	}
	else if( CurrAppScenesObj.length != 0 && CurrAppAlarmsObj.length == 0){
		CurrAppObj.push({"conf": {"home": {"floor": CurrAppHomeObj},"scenes": {"sceset": CurrAppScenesObj}}});
	}
	else if( CurrAppScenesObj.length == 0 && CurrAppAlarmsObj.length != 0){
		CurrAppObj.push({"conf": {"home": {"floor": CurrAppHomeObj},"alarms": {"alarm": CurrAppAlarmsObj}}});
	}
	else{
		CurrAppObj.push({"conf": {"home": {"floor": CurrAppHomeObj}}});
	}
	return true;
	}else
	{
		alert("您还未配置任何参数，请配置后再保存");
		return false;
	}
}
function EncapsulateALLData()
{
	/*EncapsulateStatus代表各部分封装数据状态*/
	var EncapsulateStatus = 1;
	
	if(EncapsulateHomePathData() == false)
		EncapsulateStatus = 0;
	if(EncapsulateScenesData() == true)
		isAppSence = true;
	if(EncapsulateAlarmsData() == true)
		isAppAlarem = true;
		
	if(EncapsulateReactorData() == true)
		isDevReactor = true;
	
	return parseInt(EncapsulateStatus);
}
function EncapsulateHomePathData()
{
	var NewCurrFloorObj = [];
	var NewCurrPathObj = [];
	$.each(CurrFloorArry, function (indexFloor, itemFloor) {
									
		var Elemfloorid = itemFloor.id;
		
		var CurrFloorAreaList=[];
		CurrFloorAreaList=$.grep(CurrAreaArry,function(itemCurrFloorAreaList, indexCurrFloorAreaList){
													   
			if(itemCurrFloorAreaList.floorid == Elemfloorid)
				return true;
		},false);
		
		var NewCurrFloorAreaObj = [];
		$.each(CurrFloorAreaList, function (indexArea, itemArea) {
				
				var Elemareaatype = itemArea.id;
				var CurrFloorAreaElemList=[];
				CurrFloorAreaElemList=$.grep(CurrElemArry,function(itemElem, indexElem){
					if(itemElem.floorid == Elemfloorid && itemElem.atype == Elemareaatype)
						return true;
				},false);
				
				var NewCurrFloorAreaElem = [];
				$.each(CurrFloorAreaElemList, function (index, item) {
					var Elemid = item.epid;
					var CurrFloorAreaElemCMDList=[];
					var CurrElemCMD=[];
					
					CurrFloorAreaElemCMDList=$.grep(CurrElemCMDArry,function(itemElemCMD, indexElemCMD){
						if(itemElemCMD.epid == Elemid)
							return true;
					},false);
					
					if(CurrFloorAreaElemCMDList.length != 0){
						var newMcmd = [];
						var newCcmd = [];
						var isMcmd = false;
						var isCcmd = false;
						var ctype;
						var eid;
						var rw;
						$.each(CurrFloorAreaElemCMDList, function (indexCurrElemCMD, itemCurrElemCMD) {
								eid = itemCurrElemCMD.epid;
								ctype = itemCurrElemCMD.ctype;
								rw = itemCurrElemCMD.rw;
								if(itemCurrElemCMD.hasOwnProperty("mcmd")){
									if(itemCurrElemCMD.mcmd.i == 0)
										isMcmd = false;
									else{
										isMcmd = true;
										var mcmdi = itemCurrElemCMD.mcmd.i;
										if(typeof(mcmdi) == "object" && mcmdi.constructor == Array){
											$.each(itemCurrElemCMD.mcmd.i,function(n,value){
												newMcmd.push({"@id": value.id,"@name": value.name,"@dtype": value.dtype,"@oid": value.oid,"@val": value.val});
												
											})
										}
										else{
											newMcmd.push({"@id": mcmdi.id,"@name": mcmdi.name,"@dtype": mcmdi.dtype,"@oid": mcmdi.oid,"@val": mcmdi.val});
										}
									}
								}
								if(itemCurrElemCMD.hasOwnProperty("ccmd")){
									if(itemCurrElemCMD.ccmd.i == 0)
										isCcmd = false;
									else{
										isCcmd = true;
										var ccmdi = itemCurrElemCMD.ccmd.i;
										if(typeof(ccmdi) == "object" && ccmdi.constructor == Array){
											$.each(itemCurrElemCMD.ccmd.i,function(n,value){
												newCcmd.push({"@id": value.id,"@name": value.name,"@dtype": value.dtype,"@oid": value.oid,"@val": value.val});
												
											})
										}
										else{
											newCcmd.push({"@id": ccmdi.id,"@name": ccmdi.name,"@dtype": ccmdi.dtype,"@oid": ccmdi.oid,"@val": ccmdi.val});
										}
									}
								}
								
						})
						if(isMcmd == true && isCcmd == true){
							var checkStr = item.model.substring(0,6);
							if(checkStr == '0x0102')
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename,"@shape": item.shape,"@count": item.count,"@distrib": item.distrib,"@dir": item.dir}, "cmd": {"@epid": eid,"@ctype": ctype,"@rw": rw,"ccmd": {"i": newCcmd},"mcmd": {"i": newMcmd}}});
							else
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename}, "cmd": {"@epid": eid,"@ctype": ctype,"@rw": rw,"ccmd": {"i": newCcmd},"mcmd": {"i": newMcmd}}});
						}
						else if(isMcmd == true && isCcmd == false){
							var checkStr = item.model.substring(0,6);
							if(checkStr == '0x0102')
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename,"@shape": item.shape,"@count": item.count,"@distrib": item.distrib,"@dir": item.dir}, "cmd": {"@epid": eid,"@ctype": ctype,"@rw": rw,"mcmd": {"i": newMcmd}}});
							else
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename}, "cmd": {"@epid": eid,"@ctype": ctype,"@rw": rw,"mcmd": {"i": newMcmd}}});
						}
						else if(isMcmd == false && isCcmd == true){
							var checkStr = item.model.substring(0,6);
							if(checkStr == '0x0102')
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename,"@shape": item.shape,"@count": item.count,"@distrib": item.distrib,"@dir": item.dir}, "cmd": {"@epid": eid,"@ctype": ctype,"@rw": rw,"ccmd": {"i": newCcmd}}});
							else
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename}, "cmd": {"@epid": eid,"@ctype": ctype,"@rw": rw,"ccmd": {"i": newCcmd}}});
						}
						else{
							var checkStr = item.model.substring(0,6);
							if(checkStr == '0x0102')
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename,"@shape": item.shape,"@count": item.count,"@distrib": item.distrib,"@dir": item.dir}});
							else
								NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename}});
						}
					}
					
					else{
						var checkStr = item.model.substring(0,6);
						if(checkStr == '0x0102')
							NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename,"@shape": item.shape,"@count": item.count,"@distrib": item.distrib,"@dir": item.dir}});
						else
							NewCurrFloorAreaElem.push({"@id":item.epid,"view": {"@etype": item.etype,"@name": item.etypename}});
					}
					if( item.ctrltype == 3)
						
						NewCurrPathObj.push({"@epid": item.epid,"@ctrlid": item.ctrlid,"@ctrltype": item.ctrltype,"@dchan": item.dchan,"@model": item.model,"@addr1": item.addr1,"@addr2": item.addr2});
					else
						
						NewCurrPathObj.push({"@epid": item.epid,"@ctrlid": item.ctrlid,"@ctrltype": item.ctrltype,"@devtype": item.devtype,"@dchan": item.dchan,"@model": item.model,"@addr1": item.addr1,"@addr2": item.addr2});
					
				})
				if(NewCurrFloorAreaElem.length != 0)
					NewCurrFloorAreaObj.push({"@id": itemArea.id,"@atype": itemArea.atype, "@name": itemArea.name,"elem": NewCurrFloorAreaElem});
				else
					NewCurrFloorAreaObj.push({"@id": itemArea.id,"@atype": itemArea.atype, "@name": itemArea.name});
		})
		
		if(NewCurrFloorAreaObj.length != 0)
			NewCurrFloorObj.push({"@id": itemFloor.id,"@name": itemFloor.name,"area": NewCurrFloorAreaObj});
		else
			NewCurrFloorObj.push({"@id": itemFloor.id,"@name": itemFloor.name});
			
	
		
	})
	
	CurrAppHomeObj = NewCurrFloorObj;
	CurrDevPathObj = NewCurrPathObj;

	return true;
	/*if(CurrAppHomeObj.length != 0 && CurrDevPathObj.length != 0)
		return true;
	else
		return false;*/
	
}
function EncapsulateScenesData()
{
	if(CurrScenesArry.length != 0){
		var newCurrAppScenesObj = [];
		isSence = true;
		$.each(CurrScenesArry,function(index,item){
			var newCurrElemScenes = [];
			if(item.hasOwnProperty("scene")&&item.scene!=undefined){
			var NewSences = item.scene;
			if(typeof(NewSences) == "object" && NewSences.constructor == Array){
				$.each(item.scene,function(n,value){
					var newCurrScenesI = [];
					var isSence = false;
					if(value.hasOwnProperty("i")){
						if(value.i == 0)
							isSence = false;
						else{
							isSence = true;
							var NewSencesI = value.i;
							if(typeof(NewSencesI) == "object" && NewSencesI.constructor == Array){
								$.each(NewSencesI,function(subindex,subvalue){
									newCurrScenesI.push({"@epid": subvalue.epid,"@oid": subvalue.oid,"@val": subvalue.val,"@delay": subvalue.delay});
								})
							}else{
								newCurrScenesI.push({"@epid": NewSencesI.epid,"@oid": NewSencesI.oid,"@val": NewSencesI.val,"@delay": NewSencesI.delay});
							}
						}
					}
					if(isSence == true)
						newCurrElemScenes.push({"@id": value.id,"@name": value.name,"i": newCurrScenesI});
					else
						newCurrElemScenes.push({"@id": value.id,"@name": value.name});
				})
			}else{
				var newCurrScenesI = [];
					var isSence = false;
					if(NewSences.hasOwnProperty("i")){
						if(NewSences.i == 0)
							isSence = false;
						else{
							isSence = true;
							var NewSencesI = NewSences.i;
							if(typeof(NewSencesI) == "object" && NewSencesI.constructor == Array){
								$.each(NewSencesI,function(subindex,subvalue){
									newCurrScenesI.push({"@epid": subvalue.epid,"@oid": subvalue.oid,"@val": subvalue.val,"@delay": subvalue.delay});
								})
							}else{
								newCurrScenesI.push({"@epid": NewSencesI.epid,"@oid": NewSencesI.oid,"@val": NewSencesI.val,"@delay": NewSencesI.delay});
							}
						}
					}
					if(isSence == true)
						newCurrElemScenes.push({"@id": NewSences.id,"@name": NewSences.name,"i": newCurrScenesI});
					else
						newCurrElemScenes.push({"@id": NewSences.id,"@name": NewSences.name});
			}
			if(newCurrElemScenes.length != 0)
				newCurrAppScenesObj.push({ "@id": item.id,"@floorid": item.floorid,"@aredid": item.aredid,"@name": item.name,"scene": newCurrElemScenes});
			else
				newCurrAppScenesObj.push({ "@id": item.id,"@floorid": item.floorid,"@aredid": item.aredid,"@name": item.name});
			}else{
				newCurrAppScenesObj.push({ "@id": item.id,"@floorid": item.floorid,"@aredid": item.aredid,"@name": item.name});
			}
		})
		CurrAppScenesObj = newCurrAppScenesObj;
		
		if(CurrAppScenesObj.length != 0)
			return true;
		else
			return false;
	}
	else
		return false;
}
function EncapsulateReactorData()
{
	if(CurrElemTriggerArry.length != 0){
		var newCurrTriggerObj = [];
		
		$.each(CurrElemTriggerArry,function(index,item){
			
			var isTrigger = false;
			if(item.hasOwnProperty("i")){
				
				if(item.i == 0){
					isTrigger = false;
					newCurrTriggerObj.push({ "@rid": item.rid,"@t_epid": item.t_epid,"@val": item.val,"@oid": item.oid});
				}
				else{
					isTrigger = true;
					var NewtriggerI = item.i;
					var newCurrTriggerI = [];
					if(typeof(NewtriggerI) == "object" && NewtriggerI.constructor == Array){
						
						$.each(item.i,function(subindex,subvalue){
								newCurrTriggerI.push({"@e_epid": subvalue.e_epid,"@oid": subvalue.oid,"@val": subvalue.val,"@delay": subvalue.delay});
						})
						
					}
					else{
						newCurrTriggerI.push({"@e_epid": NewtriggerI.e_epid,"@oid": NewtriggerI.oid,"@val": NewtriggerI.val,"@delay": NewtriggerI.delay});
						
					}
					newCurrTriggerObj.push({ "@rid": item.rid,"@t_epid": item.t_epid,"@val": item.val,"@oid": item.oid,"i": newCurrTriggerI});
				}
			}
			else{
				newCurrTriggerObj.push({ "@rid": item.rid,"@t_epid": item.t_epid,"@val": item.val,"@oid": item.oid});
			}
						
		})
		CurrDevReactorObj = newCurrTriggerObj;
		
		if(CurrDevReactorObj.length != 0)
			return true;
		else
			return false;

	}	
	else
		return false;
}


function EncapsulateAlarmsData()
{
	
if(CurrAlarmsArry.length != 0){
	var newCurrAppAlarmsObj = [];
	
	$.each(CurrAlarmsArry,function(index,item){
	var newCurrAlarmAreas = [];
	var isArea = true;
	if(item.hasOwnProperty("area")){
		if(item.area == 0)
			isArea = false;
		else{
			isArea = true;
			
			var NewAreas = item.area;
			
			if(typeof(NewAreas) == "object" && NewAreas.constructor == Array){
				$.each(NewAreas,function(n,value){
					var newCurrAreasZone = [];
					var isZone = false;
					//if(value.zone != undefined && value.zone != null){
					if(value.hasOwnProperty("zone")){
						if(value.zone == 0)
							isZone = false;
						else{
							isZone = true;
							var NewAreasZone = value.zone;
							if(typeof(NewAreasZone) == "object" && NewAreasZone.constructor == Array){
								$.each(NewAreasZone,function(subindex,subvalue){
									newCurrAreasZone.push({"@id": subvalue.id,"@name": subvalue.name});
								})
							}else{
								newCurrAreasZone.push({"@id": NewAreasZone.id,"@name": NewAreasZone.name});
								
							}
						}
					}
					if(isZone == true)
						newCurrAlarmAreas.push({"@id": value.id,"@name": value.name,"zone": newCurrAreasZone});
					else
						newCurrAlarmAreas.push({"@id": value.id,"@name": value.name});
				})
			}else{
				var newCurrAreasZone = [];
					var isZone = false;
					//if(NewAreas.zone != undefined && NewAreas.zone != null){
					if(NewAreas.hasOwnProperty("zone")){
						if(NewAreas.zone == 0)
							isZone = false;
						else{
							isZone = true;
							var NewAreasZone = NewAreas.zone;
							if(typeof(NewAreasZone) == "object" && NewAreasZone.constructor == Array){
								$.each(NewAreasZone,function(subindex,subvalue){
									newCurrAreasZone.push({"@id": subvalue.id,"@name": subvalue.name});
								})
							}else{
								newCurrAreasZone.push({"@id": NewAreasZone.id,"@name": NewAreasZone.name});
							}
						}
					}
					if(isZone == true)
						newCurrAlarmAreas.push({"@id": NewAreas.id,"@name": NewAreas.name,"zone": newCurrAreasZone});
					else
						newCurrAlarmAreas.push({"@id": NewAreas.id,"@name": NewAreas.name});
			}
			
		
	  }
		if(newCurrAlarmAreas.length != 0 && isArea == true)
			newCurrAppAlarmsObj.push({ "@id": item.id,"@type": item.type,"@epid": item.epid,"@name": item.name,"area": newCurrAlarmAreas});
		else
			newCurrAppAlarmsObj.push({ "@id": item.id,"@type": item.type,"@epid": item.epid,"@name": item.name});
	
	}else{//no area
		newCurrAppAlarmsObj.push({ "@id": item.id,"@type": item.type,"@epid": item.epid,"@name": item.name});
	}
	})
	CurrAppAlarmsObj = newCurrAppAlarmsObj;
	
	if(CurrAppAlarmsObj.length != 0)
		return true;
	else
		return false;
}
	else
		return false;
}